<?php


/**
 * Controller for example application 2.
 */
class Controller_App2 extends Psa_Plugin_Controller{
	
	/**
	 * Reference to result object
	 * @var Psa_Result
	 */
	protected $psa_result;


	/**
	 * Main controller method.
	 * This method is called by router.
	 * @param array $url_args Action name. Parts of URL path.
	 */
	function psa_main($url_args){

		// global PSA config array
		global $PSA_CFG;
		
		// take action method name from url
		$action_method = trim(@$url_args[0]);
		
		// put reference to (this) controller object into the registry
		Psa_Registry::get_instance()->Web_Controller = &$this;
		
		// reference to result object
		$this->psa_result = Psa_Result::get_instance();
		
		// check if this is an ajax request
		if(@$_SERVER["HTTP_X_REQUESTED_WITH"] == 'XMLHttpRequest')
			$this->psa_result->ajax_request = 1;
		else
			$this->psa_result->ajax_request = 0;
			
		// put some values in result object to be available in all templates
		$this->psa_result->basedir_web = $PSA_CFG['folders']['basedir_web'] . '/app2';
		$this->psa_result->page_title = 'Example application 2';
		
		// .css and .js files. Use absolute url path due to rewriting urls
		$this->psa_result->include_css[] = $PSA_CFG['folders']['basedir_web'] . "/examples/example_app2/css/styles.css";
		$this->psa_result->include_js[] = $PSA_CFG['folders']['basedir_web'] . "/examples/example_app2/js/mootools-1.2.1-core.js";
		$this->psa_result->include_js[] = $PSA_CFG['folders']['basedir_web'] . "/examples/example_app2/js/functions.js";
		
		// include all action methods
		include_once 'Web_Action_Methods.php';
		
		// make new object with action methods
		$web_action_methods = new Web_Action_Methods($this);
		
		// check if we have defined this action, if not set index action by default
		if(!method_exists($web_action_methods, $action_method))
			$action_method = 'index';
			
		// call action and if Unauthorized_Access_Exception raised call login action 
		try{
			$web_action_methods->$action_method($url_args);
		}
		catch(Unauthorized_Access_Exception $e){
			$web_action_methods->login($url_args);
		}
	}


	/**
	 * Runs plugins.
	 * This method is used in action methods for calling plugins.
	 * @param array $run_plugins argument for psa_run_plugins() function.
	 */
	function run_plugins($run_plugins){

		// first run model plugins and catch some exceptions
		try{
			if(isset($run_plugins['model']))
				psa_run_plugins($run_plugins['model']);
		}
		// if validation exception raised
		catch(Psa_Validation_Exception $e){
			$run_plugins = array(); // reset array
			$run_plugins['view']['Main_View']['location_validation_exception'] = array($e->getMessage());
		}
		// if unauthorized action exception raised
		catch(Unpermitted_Action_Exception $e){
			$run_plugins = array();
			$run_plugins['view']['Main_View']['location_unpermitted_action_exception'] = array($e->getMessage());
		}
		
		// after model plugins are called assign all results from models (every property in $this->psa_result object)
		// to Smarty ($psa_smarty) object. I use Smarty template engine in this application.
		$this->smarty_assign_result();
		
		// run view plugins at the end
		if(isset($run_plugins['view']))
			psa_run_plugins($run_plugins['view']);
	}


	/**
	 * Checks login and sets some values to registry object.
	 * This method is used in action methods for checking if user is authorized.
	 */
	function check_login(){

		// make new user object
		$user = new Psa_User();
		
		// check if we have user stored in session
		if(!$user->session_restore()){
			
			// check if login data are sent from login form
			if(isset($_POST['login_user']) && isset($_POST['login_pass'])){
				$user->username = $_POST['login_user'];
				$user->password = $_POST['login_pass'];
				
				// try to authorize user with username and password
				if($user->authorize()){
					// serialize user object into session
					$user->session_save();
				}
				// if user is not authorized set unsuccessful_authorize value in result object
				// for login form to show error message.
				else{
					$this->psa_result->unsuccessful_authorize = 1;
				}
			}
		}
		
		if($user->authorized){
			
			// put reference to user object into registry
			Psa_Registry::get_instance()->user = &$user;
			
			// value that will be available in all templates
			$this->psa_result->username = $user->username;
		}else{
			// throw Unauthorized_Access_Exception
			include_once 'examples/example_app2/exception/Unauthorized_Access_Exception.php';
			throw new Unauthorized_Access_Exception("Unauthorized access attempt.");
		}
	}
}

?>